SwapIt / app / api / product / [pid] / route.js
route.js
Raw
// GET (read)
import { op } from "@utils/user.mjs";

export const GET = async ( req, {params}) => {
    try {
        const Product = await op.findProductById(params.pid);

        if(!Product.found) return new Response("Product not found", {status: 204} )

        return new Response(JSON.stringify(Product.data) , {status:200})
        
    } catch (error) {
        return new Response("Failed to read the Products" , {status:500});
    }
}


// PAtch (update)

export const PATCH = async (req, {params}) => {
    const { NOP, type, image, desc, price} = await req.json();

    
    try {
        const Product = await op.findProductById(params.pid);
        if(!Product.found) return new Response("Product not found", {status: 204} )
        Product.data[2] = NOP;
        Product.data[3] = type;
        Product.data[4] = image;
        Product.data[5] = desc;
        Product.data[6] = price;
        await op.updateProductById(Product.data,params.pid)
        return new Response(JSON.stringify(Product.data) , {status:200})

    } catch (error) {
        return new Response("Failed to read the Products" , {status:500});
    }

}


// Delete

export const DELETE = async (request , {params}) => {
    try {
        await op.DeleteProductById(params.pid);

        return new Response("Deleted the Products" , {status:200});
        
    } catch (error) {
        return new Response("Failed to delete the Products" , {status:500});
    }
}